/*******************************************************************************
 * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
 * @file     triangle.c
 * @author   UartTFT Application Team
 * @version  V0.0.1
 * @date     2024-02-22
 * @brief
 ********************************************************************************/

#include "triangle.h"
#if 0
int min(int v1,int v2)
{
      if(v1>=v2) return v2;
      else       return v1;
}
int max(int v1,int v2)
{
      if(v1>=v2) return v1;
      else       return v2;
}
#endif
uint16_t Source_Width;
uint16_t Source_Height;

uint16_t NewPic_Width;
uint16_t NewPic_Height;

void PicXZ(unsigned short *Source,unsigned short *NewPic,int angle)
{
#if 0
	int id;
	int i;

	if(angle==360) angle = 0;
	id = angle/5;

	int cosine = cosbeta[id];
	int sine   = sinbeta[id];
	int Point1x=(-Source_Height*sine)/10000;
	int Point1y=(Source_Height*cosine)/10000;
	int Point2x=(Source_Width*cosine-Source_Height*sine)/10000;
	int Point2y=(Source_Height*cosine+Source_Width*sine)/10000;
	int Point3x=(Source_Width*cosine)/10000;
	int Point3y=(Source_Width*sine)/10000;
	int minx=min(0,min(Point1x,min(Point2x,Point3x)));
	int miny=min(0,min(Point1y,min(Point2y,Point3y)));
	int maxx=max(Point1x,max(Point2x,Point3x));
	int maxy=max(Point1y,max(Point2y,Point3y));
	int DestBitmapWidth,DestBitmapHeight;

	if(angle>90&&angle<180)
		DestBitmapWidth= -minx;
	else
		DestBitmapWidth=maxx-minx;

	if(angle>180&&angle<270)
		DestBitmapHeight=-miny;
	else
		DestBitmapHeight=maxy-miny;

//	NewPic_Height=DestBitmapHeight;
//	NewPic_Width=DestBitmapWidth;

	for(int x=0;x<DestBitmapWidth;x++)
	{
		for(int y=0;y<DestBitmapHeight;y++)
		{
			  int SrcBitmapx=((x+minx)*cosine+(y+miny)*sine)/10000;
			  int SrcBitmapy=((y+miny)*cosine-(x+minx)*sine)/10000;
			  if(SrcBitmapx>=0&&SrcBitmapx<Source_Width&&SrcBitmapy>=0&&
					SrcBitmapy<Source_Height)
			  {
	//                        NewPic->Canvas->Pixels[x][y]=Source->Canvas->Pixels[SrcBitmapx][SrcBitmapy];
	//						NewPic[x][y]=Source[SrcBitmapx][SrcBitmapy];
					NewPic[x+y*DestBitmapWidth]=Source[SrcBitmapx+SrcBitmapy*Source_Width];
			  }
		}
	}

	NewPic_Height=DestBitmapHeight;
	NewPic_Width=DestBitmapWidth;
#endif
}



static float cosbeta[72]={
	10000,		//Ang=0
	9961,		//Ang=5
	9848,		//Ang=10
	9659,		//Ang=15
	9396,		//Ang=20
	9063,		//Ang=25
	8660,		//Ang=30
	8191,		//Ang=35
	7660,		//Ang=40
	7071,		//Ang=45
	6427,		//Ang=50
	5735,		//Ang=55
	5000,		//Ang=60
	4226,		//Ang=65
	3420,		//Ang=70
	2588,		//Ang=75
	1736,		//Ang=80
	871,		//Ang=85
	0,		//Ang=90
	-871,		//Ang=95
	-1736,	//Ang=100
	-2588,	//Ang=105
	-3420,	//Ang=110
	-4226,	//Ang=115
	-5000,	//Ang=120
	-5735,	//Ang=125
	-6427,	//Ang=130
	-7071,	//Ang=135
	-7660,	//Ang=140
	-8191,	//Ang=145
	-8660,	//Ang=150
	-9063,	//Ang=155
	-9396,	//Ang=160
	-9659,	//Ang=165
	-9848,	//Ang=170
	-9961,	//Ang=175
	-10000,		//Ang=180
	-9961,	//Ang=5
	-9848,	//Ang=10
	-9659,	//Ang=15
	-9396,	//Ang=20
	-9063,	//Ang=25
	-8660,	//Ang=30
	-8191,	//Ang=35
	-7660,	//Ang=40
	-7071,	//Ang=45
	-6427,	//Ang=50
	-5735,	//Ang=55
	-5000,	//Ang=60
	-4226,	//Ang=65
	-3420,	//Ang=70
	-2588,	//Ang=75
	-1736,	//Ang=80
	-871,		//Ang=85
	0,		//Ang=90
	871,		//Ang=95
	1736,		//Ang=100
	2588,		//Ang=105
	3420,		//Ang=110
	4226,		//Ang=115
	5000,		//Ang=120
	5735,		//Ang=125
	6427,		//Ang=130
	7071,		//Ang=135
	7660,		//Ang=140
	8191,		//Ang=145
	8660,		//Ang=150
	9063,		//Ang=155
	9396,		//Ang=160
	9659,		//Ang=165
	9848,		//Ang=170
	9961,		//Ang=175
	};

static float sinbeta[72]={
	0,		//Ang=0
	871,		//Ang=5
	1736,		//Ang=10
	2588,		//Ang=15
	3420,		//Ang=20
	4226,		//Ang=25
	5000,		//Ang=30
	5735,		//Ang=35
	6427,		//Ang=40
	7071,		//Ang=45
	7660,		//Ang=50
	8191,		//Ang=55
	8660,		//Ang=60
	9063,		//Ang=65
	9396,		//Ang=70
	9659,		//Ang=75
	9848,		//Ang=80
	9961,		//Ang=85
	10000,		//Ang=90
	9961,		//Ang=95
	9848,		//Ang=100
	9659,		//Ang=105
	9396,		//Ang=110
	9063,		//Ang=115
	8660,		//Ang=120
	8191,		//Ang=125
	7660,		//Ang=130
	7071,		//Ang=135
	6427,		//Ang=140
	5735,		//Ang=145
	5000,		//Ang=150
	4226,		//Ang=155
	3420,		//Ang=160
	2588,		//Ang=165
	1736,		//Ang=170
	871,		//Ang=175
	0,		//Ang=180
	-871,		//Ang=5
	-1736,	//Ang=10
	-2588,	//Ang=15
	-3420,	//Ang=20
	-4226,	//Ang=25
	-5000,	//Ang=30
	-5735,	//Ang=35
	-6427,	//Ang=40
	-7071,	//Ang=45
	-7660,	//Ang=50
	-8191,	//Ang=55
	-8660,	//Ang=60
	-9063,	//Ang=65
	-9396,	//Ang=70
	-9659,	//Ang=75
	-9848,	//Ang=80
	-9961,	//Ang=85
	-10000,	//Ang=90
	-9961,	//Ang=95
	-9848,	//Ang=100
	-9659,	//Ang=105
	-9396,	//Ang=110
	-9063,	//Ang=115
	-8660,	//Ang=120
	-8191,	//Ang=125
	-7660,	//Ang=130
	-7071,	//Ang=135
	-6427,	//Ang=140
	-5735,	//Ang=145
	-5000,	//Ang=150
	-4226,	//Ang=155
	-3420,	//Ang=160
	-2588,	//Ang=165
	-1736,	//Ang=170
	-871,		//Ang=175
	};



#if 0    // debug
{
	LCD_BL_Init(249);

	EPORT2->EPDR &=~(1<<7); // speed test

	Source_Width = 128;
	Source_Height = 128;

	LT268_pic_to_buffer(0,0,Source_Width,m_PngData, //dispaly_ram
			0,0,Source_Width,0x000,
			Source_Width,Source_Height,0);
//		LT268_buffer_to_TFT(0,0,Source_Width,m_PngData,
//			0,0,Source_Width,Source_Height);
	for(uint16_t aa=0;aa<=360;aa+=5)
	{
		if(aa==360) aa=0;

		PicXZ((uint16_t*)m_PngData,(uint16_t*)dispaly_ram,aa);

		LT268_buffer_to_TFT(0,0,NewPic_Width,dispaly_ram,
			200+(181-NewPic_Width)/2,0+(181-NewPic_Height)/2,NewPic_Width,NewPic_Height);
		DelayMS(20);
	}
	EPORT2->EPDR |= (1<<7);  // 10MS

	while(1);
}
#endif
